<?php

/**
 * LEAGUEMan
 *
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class Roundresult
{
    /** @var object project */
    public $project;
    /** @var object round */
    public $round;

    public function __construct()
    {
        $this->project = null;
        $this->round = null;
    }

    public function canEdit($id)
    {
        return ($this->project->getResponsible() == intval($id));
    }

    public function setProject(LM_Table_Project $project)
    {
        $this->project = $project;
    }

    public function setRound(LM_Table_Round $round)
    {
        $this->round = $round;
    }

    public function loadProject($id)
    {
        $this->project = LM::getLMTable("Project");
        $this->project->load($id);
    }

    public function loadRound($id)
    {
        $this->round = LM::getLMTable("Round");
        $this->round->load($id);
    }

    public function loadRoundByOrdering($ordering = 0)
    {
        $this->round = LM::getLMTable("Round");

        if ($ordering == 0) {
            $ordering = $this->project->getCurrentRound();
        }

        if (!$this->round->loadByOrdering($this->project->getID(), $ordering)) {
            $this->round = null;
        }
    }

    public function getRound()
    {
        return $this->round;
    }

    public function getProject()
    {
        return $this->project;
    }

    public function getResults($hit = true)
    {
        if ($hit) {
            $this->round->hit();
        }

        $matchtable = LM::getLMTable("Match");
        $matchtable->filterbyProject($this->project->getID());
        $matchtable->filterbyRound($this->round->getOrdering());

        return $matchtable->getRows();
    }

}